<template>
  <div class="box-card">
    <el-input
      v-model.trim="orgName"
      size="small"
      prefix-icon="el-icon-search"
      placeholder="请搜索组织名称"
      maxlength="30"
      clearable
      style="margin: 16px 0 0 0;"
    />
    <!-- <div class="left-title" /> -->
    <!-- <div class="treeTop">
      <div class="treetop-left">
        <svg-icon icon-class="grotree" />
        <span>{{ selectedOrg.name }}</span>
      </div>
      <el-switch
        v-model="switchvalue"
        active-text="包含下级"
      />
    </div> -->

    <el-tree
      ref="tree"
      :empty-text="$store.getters.dataText"
      class="left-tree"
      :data="tree"
      :load="load"
      lazy
      :props="defaultProps"
      :default-expand-all="false"
      :expand-on-click-node="false"
      :highlight-current="true"
      :current-node-key="currentKey"
      :filter-node-method="filterNode"
      node-key="id"
      @node-click="handleNodeClick"
    >
      <div slot-scope="{ data }" class="custom-tree-node">
        <svg-icon icon-class="organization" />
        <DtOverTooltip :content="data.name" />
      </div>
    </el-tree>
  </div>
</template>
<script>
import bussinessUserAuthorizationApi from '@/framework/api/authCenter/bussinessUserAuthorization/bussinessUserAuthorization'

export default {
  name: 'GroTree',
  props: {
    name: {
      type: String,
      default: ''
    },
    handle: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      switchvalue: true,
      selectedOrg: '',
      orgName: '',
      tree: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      currentKey: ''
    }
  },
  watch: {
    orgName(val) {
      let params = {}
      if (val) {
        params = {
          orgId: '', orgName: val
        }
      } else {
        params = {
          orgId: '-1'
        }
      }
      bussinessUserAuthorizationApi.getOrgTree(params).then((res) => {
        const data = res.data.map((item, index) => {
          // item.orgId = item.id
          // item.orgName = item.name
          return item
        })
        this.tree = data
      })
    }
  },
  created() {
    this.initTree()
  },
  methods: {

    // // 懒加载
    load(tree, resolve) {
      let orgid = ''
      if (!tree.data || tree.data.length === 0) {
        orgid = '-1'
        return
      } else {
        orgid = tree.data.id
      }
      bussinessUserAuthorizationApi.getOrgTree({ orgId: orgid }).then((res) => {
        const data = res.data.map((item, index) => {
          return item
        })
        this.selectedOrg = res.data[0]
        // this.currentKey = res.data[0].id
        this.$nextTick(() => {
          // this.$refs.tree.setCurrentKey(res.data[0].id)
        })
        resolve(data)
      })
    },
    /**
       * 选中树节点
       */
    handleNodeClick(data) {
      this.selectedOrg = data
      this.$emit('treeClick', data)
    },
    // 获取左侧组织树
    initTree() {
      const that = this
      bussinessUserAuthorizationApi.getOrgTree({ orgId: '-1' }).then((res) => {
        that.tree = res.data
        this.selectedOrg = res.data[0]
        this.currentKey = res.data[0].id
        this.$emit('treeClick', res.data[0])
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(res.data[0].id)
        })
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    }
  }

}
</script>
  <style lang="scss" scoped>
  .cardHeader {
    .headSpan {
      font-size: 16px;
      font-family: HarmonyOS Sans SC-Medium, HarmonyOS Sans SC;
      font-weight: 500;
      color: #131414;
      line-height: 24px;
    }

    .headBtn {
      float: right;
      border-radius: 4px 4px 4px 4px;
      opacity: 1;
      line-height: 24px;
      padding: 0;
    }
  }

  .svg-icon {
    font-size: 24px;
    display: block;
    margin-right: 8px !important;
    float: none !important;
  }
  .treeTop{
    display: flex;
    align-items: center;
    justify-content: space-between;
    .treetop-left{
      display: flex;
      align-items: center;
    }
  }

  .left-title {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin-bottom: 16px;
  }

  .left-tree {
    margin-top: 23px;
  }

  ::v-deep .el-card__header {
    padding: 0;
    border-bottom: none;
  }

  ::v-deep .el-card__body {
    padding: 0;
  }

  ::v-deep .el-dropdown-menu__item {
    padding: 6px 16px;
    line-height: 24px;
  }

  .custom-tree-node {
    display: flex;
    align-items: center;
    overflow: hidden;
    width: 100%;

    .tree-name {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      flex: 1;
    }

    .btn {
      color: var(--themeColor);

      i {
        margin-right: 8px;
        font-size: 16px;
      }
    }

    .el-dropdown-link {
      margin-right: 8px;
      display: block;
      width: 16px;
      height: 16px;
      text-align: center;
    }

    .el-dropdown-link:hover {
      color: var(--themeColor);
    }
  }
  </style>

